package com.xiaok.mobileredis.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaok.mobileredis.entity.SensitiveWord;
import com.xiaok.mobileredis.enums.StatusCode;
import com.xiaok.mobileredis.request.SensitiveWordReq;
import com.xiaok.mobileredis.response.BaseResponse;
import com.xiaok.mobileredis.service.SensitiveWordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Classname SensitiveWordController
 * @Description
 * @Date 2020/9/20 22:00
 * @Created by xiaok
 */
@RestController
@RequestMapping("/sensitiveword")
public class SensitiveWordController {

    private static final Logger logger = LoggerFactory.getLogger(SensitiveWordController.class);
    @Resource
    SensitiveWordService sensitiveWordService;

    /**
     *添加一条数据
     * @param
     * @param result
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<String> add(@RequestBody @Valid SensitiveWordReq sensitiveWordReq,BindingResult result){
         logger.info("param  :" + sensitiveWordReq.toString());
            if(result.hasErrors()){
                return new BaseResponse<>(StatusCode.InvalidParams);
            }
        //校验成功,添加数据
        BaseResponse<String> response = new BaseResponse<>(StatusCode.Success);
        try {
            SensitiveWord sensitiveWord = new SensitiveWord();
            sensitiveWord.setIsActive(sensitiveWordReq.getIsActive());
            sensitiveWord.setWord(sensitiveWordReq.getWord());
            Integer id = sensitiveWordService.add(sensitiveWord);
            response.setData(String.valueOf(id));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("发送异常  :" +e.getMessage());
            response.setCode(StatusCode.Fail.getCode());
            response.setMsg("添加过滤词数据失败");
            return response;
        }
        return response ;
    }

    /**
     * 删除过滤词失败
     * @param id
     * @param
     * @return
     */
    @GetMapping("/delete/{id}")
    public BaseResponse<String> delete(@PathVariable(value = "id",required = true) Integer id){
        logger.info("param  :" + JSON.toJSONString(id));
        // TODO 权限校验
        logger.info("222222222");
        //校验成功,添加数据
        BaseResponse<String> response =new BaseResponse<>(StatusCode.Success);
        response.setMsg("删除过滤词成功");
        try {
            sensitiveWordService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("发送异常  :" +e.getMessage());
            response.setCode(StatusCode.Fail.getCode());
            response.setMsg("删除过滤词失败");
            return response;
        }
        return  response ;
    }

    /**
     *
     * @param sensitiveWord
     * @param result
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<String> update(@RequestBody @Valid SensitiveWord sensitiveWord, BindingResult result){
        logger.info("param  :" +  sensitiveWord.toString());
        if(result.hasErrors()){
            return new BaseResponse<>(StatusCode.InvalidParams);
        }
        // TODO 权限校验
        //校验成功,添加数据
        BaseResponse<String> response =new BaseResponse<>(StatusCode.Success);
        try {
            sensitiveWordService.update(sensitiveWord);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("发送异常  :" +e.getMessage());
            response.setCode(StatusCode.Fail.getCode());
            response.setMsg("更新过滤词列表失败");
            return response;
        }
        return  response ;
    }

    /**
     * 查询过滤词列表
     * @return
     */
    @GetMapping("/selectList")
    public BaseResponse<String> selectList(){

        // TODO 权限校验
        BaseResponse<String> response = new BaseResponse<>(StatusCode.Success);
        try {
            List<SensitiveWord> selectlist = sensitiveWordService.selectlist();
            response.setData(JSONObject.toJSON(selectlist).toString());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("发送异常  :" +e.getMessage());
            response.setCode(StatusCode.Fail.getCode());
            response.setMsg("查询过滤词列表失败");
            return response;
        }
        return  response  ;
    }



}
